#include "maindlg.h"
#include "ui_maindlg.h"
#include "mywebtab.h"

MainDlg::MainDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainDlg)
{
    ui->setupUi(this);
    // 最大化，最小化按钮
    this->setWindowFlags(this->windowFlags()|Qt::WindowMinMaxButtonsHint);
    this->setFixedSize(1024,600);
    // CurrentPath
    m_strRoot = QApplication::applicationDirPath();
    QDir::setCurrent(m_strRoot);

    InitOnce();
}

MainDlg::~MainDlg()
{
    delete ui;
}

void MainDlg::InitOnce()
{
    m_pWebTabMgr = new MyWebTabMgr(this);
    m_pWebTabMgr->Init(this);
}

void MainDlg::on_nav_btn_go_clicked()
{
    QString strAddr = ui->nav_edit_addr->text();
    // url
    QString tmpUrl = strAddr.toLower();
    if( !tmpUrl.startsWith("http:") && !tmpUrl.startsWith("https:") && !tmpUrl.startsWith("ftp:")&& !tmpUrl.startsWith("file:") ){
        strAddr = "http://"+strAddr;
        ui->nav_edit_addr->setText(strAddr);
    }
    m_pWebTabMgr->CreateTab(NULL, true)->setUrl(QUrl(strAddr));
}


void MainDlg::on_nav_btn_home_clicked()
{
    ui->nav_edit_addr->setText("http://www.baidu.com");
    ui->nav_btn_go->click();
}

void MainDlg::on_nav_btn_left_clicked()
{
    MyWebTab* pTab = (MyWebTab*)ui->view_tab->currentWidget();
    pTab->getWebView()->page()->history()->back();
}

void MainDlg::on_nav_btn_right_clicked()
{
    MyWebTab* pTab = (MyWebTab*)ui->view_tab->currentWidget();
    pTab->getWebView()->page()->history()->forward();
}

void MainDlg::on_toolbar_btn_new_clicked()
{
    QString strNew = m_strRoot + "/Tools/down_th_new.html";
    QUrl url = QUrl::fromLocalFile(strNew);
    ui->nav_edit_addr->setText(url.toString());
    ui->nav_btn_go->click();
}

void MainDlg::on_toolbar_btn_test_clicked()
{
    m_pWebTabMgr->m_pDownLoadSDK->CreateTaskTh("https://dldir1.qq.com/qqfile/QQIntl/QQi_PC/QQIntl2.11.exe");
}

void MainDlg::on_toolbar_btn_status_clicked()
{
    QString strNew = m_strRoot + "/Tools/down_th_status.html";
    QUrl url = QUrl::fromLocalFile(strNew);
    ui->nav_edit_addr->setText(url.toString());
    ui->nav_btn_go->click();
}
